home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / gfx / nsCoord.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  5KB  |  157 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. #ifndef NSCOORD_H
  39. #define NSCOORD_H
  40.  
  41. #include <math.h>
  42.  
  43. #include "nsDebug.h"
  44.  
  45. /*
  46.  * Basic type used for the geometry classes.
  47.  *
  48.  * Normally all coordinates are maintained in the twips coordinate
  49.  * space. A twip is 1/20th of a point, and there are 72 points per
  50.  * inch. However, nscoords do appear in pixel space and other
  51.  * coordinate spaces.
  52.  *
  53.  * Twips are used because they are a device-independent unit of measure. See
  54.  * header file nsUnitConversion.h for many useful macros to convert between
  55.  * different units of measure.
  56.  */
  57.  
  58. // This controls whether we're using integers or floats for coordinates. We
  59. // want to eventually use floats. If you change this, you need to manually
  60. // change the definition of nscoord in gfx/idl/gfxtypes.idl.
  61. //#define NS_COORD_IS_FLOAT
  62.  
  63. inline float NS_IEEEPositiveInfinity() {
  64.   float f;
  65.   *(PRUint32*)&f = 0x7F800000;
  66.   return f;
  67. }
  68. inline PRBool NS_IEEEIsNan(float aF) {
  69.   PRUint32 bits = *(PRUint32*)&aF;
  70.   return (bits & 0x7F800000) == 0x7F800000 &&
  71.     (bits & 0x007FFFFF) != 0;
  72. }
  73.  
  74. #ifdef NS_COORD_IS_FLOAT
  75. typedef float nscoord;
  76. #define nscoord_MAX NS_IEEEPositiveInfinity()
  77. #else
  78. typedef PRInt32 nscoord;
  79. #define nscoord_MAX nscoord(1 << 30)
  80. #endif
  81.  
  82. #define nscoord_MIN (-nscoord_MAX)
  83.  
  84. inline void VERIFY_COORD(nscoord aCoord) {
  85. #ifdef NS_COORD_IS_FLOAT
  86.   NS_ASSERTION(floorf(aCoord) == aCoord,
  87.                "Coords cannot have fractions");
  88. #endif
  89. }
  90.  
  91. inline nscoord NSCoordMultiply(nscoord aCoord, float aVal) {
  92.   VERIFY_COORD(aCoord);
  93. #ifdef NS_COORD_IS_FLOAT
  94.   return floorf(aCoord*aVal);
  95. #else
  96.   return (PRInt32)(aCoord*aVal);
  97. #endif
  98. }
  99.  
  100. inline nscoord NSCoordMultiply(nscoord aCoord, PRInt32 aVal) {
  101.   VERIFY_COORD(aCoord);
  102.   return aCoord*aVal;
  103. }
  104.  
  105. inline nscoord NSCoordDivide(nscoord aCoord, float aVal) {
  106.   VERIFY_COORD(aCoord);
  107. #ifdef NS_COORD_IS_FLOAT
  108.   return floorf(aCoord/aVal);
  109. #else
  110.   return (PRInt32)(aCoord/aVal);
  111. #endif
  112. }
  113.  
  114. inline nscoord NSCoordDivide(nscoord aCoord, PRInt32 aVal) {
  115.   VERIFY_COORD(aCoord);
  116. #ifdef NS_COORD_IS_FLOAT
  117.   return floorf(aCoord/aVal);
  118. #else
  119.   return aCoord/aVal;
  120. #endif
  121. }
  122.  
  123. /**
  124.  * Convert an nscoord to a PRInt32. This *does not* do rounding because
  125.  * coords are never fractional. They can be out of range, so this does
  126.  * clamp out of bounds coord values to PR_INT32_MIN and PR_INT32_MAX.
  127.  */
  128. inline PRInt32 NSCoordToInt(nscoord aCoord) {
  129.   VERIFY_COORD(aCoord);
  130. #ifdef NS_COORD_IS_FLOAT
  131.   NS_ASSERTION(!NS_IEEEIsNan(aCoord), "NaN encountered in int conversion");
  132.   if (aCoord < -2147483648.0f) {
  133.     // -2147483648 is the smallest 32-bit signed integer that can be
  134.     // exactly represented as a float
  135.     return PR_INT32_MIN;
  136.   } else if (aCoord > 2147483520.0f) {
  137.     // 2147483520 is the largest 32-bit signed integer that can be
  138.     // exactly represented as an IEEE float
  139.     return PR_INT32_MAX;
  140.   } else {
  141.     return (PRInt32)aCoord;
  142.   }
  143. #else
  144.   return aCoord;
  145. #endif
  146. }
  147.  
  148. inline float NSCoordToFloat(nscoord aCoord) {
  149.   VERIFY_COORD(aCoord);
  150. #ifdef NS_COORD_IS_FLOAT
  151.   NS_ASSERTION(!NS_IEEEIsNan(aCoord), "NaN encountered in float conversion");
  152. #endif
  153.   return (float)aCoord;
  154. }
  155.  
  156. #endif /* NSCOORD_H */
  157.